package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.mapper.PictureMapper;
import cn.tedu.csmall.product.mapper.SpuMapper;
import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Album;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AlbumServiceImpl implements IAlbumService {

    @Autowired
    private AlbumMapper albumMapper;
    @Autowired
    private PictureMapper pictureMapper;
    @Autowired
    private SpuMapper spuMapper;

    public AlbumServiceImpl(){
        log.debug("创建业务对象:AlbumServiceImpl");
    }

    @Override
    public void addNew(AlbumAddNewDTO albumAddnewDTO) {
        log.debug("开始处理【添加相册】的业务，参数：{}", albumAddnewDTO);
        // 应该保证此相册的的名称是唯一的
        int count = albumMapper.countByName(albumAddnewDTO.getName());
        if (count > 0){
            String message = "添加相册失败!尝试添加的相册名称已经被使用!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        // 创建Album类型的对象
        Album album = new Album();
        // 调用BeanUtils.copyProperties(源对象, 目标对象)将参数的属性值复制到新创建的Album对象中
        BeanUtils.copyProperties(albumAddnewDTO,album);
        // 调用albumMapper的int insert(Album album)方法插入数据
        log.debug("即将插入相册数据：{}",album);
        int rows = albumMapper.insert(album);
        if (rows != 1){
            String message = "添加相册失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据id删除相册】的业务，参数：{}",id);
        // 检查尝试删除的数据是否存在
        Object queryResult = albumMapper.getStandardById(id);
        if (queryResult == null){
            String message = "删除相册失败，尝试删除的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        {
            int count = pictureMapper.countByAlbumId(id);
            if (count > 0) {
                String message = "删除相册失败，存在图片关联到此相册！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }
        {
            int count = spuMapper.countByAlbumId(id);
            if (count > 0) {
                String message = "删除相册失败，存在SPU关联到此相册！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }


        // 执行删除
        log.debug("即将执行删除数据，参数：{}",id);
        int rows = albumMapper.deleteById(id);
        if (rows != 1){
            String message = "删除相册失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Long id, AlbumUpdateDTO albumUpdateDTO) {
        log.debug("开始处理【修改相册基本信息】的业务，参数ID：{}，新数据：{}", id, albumUpdateDTO);
        // 检查尝试修改的数据是否存在
        Object queryResult = albumMapper.getStandardById(id);
        if (queryResult == null){
            String message = "修改相册失败，尝试修改的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        //检查名称是否与其他的相册数据冲突
        int count = albumMapper.countByNameAndNotId(id, albumUpdateDTO.getName());
        if (count>0){
            String message = "修改相册基本信息失败，相册名称已被其他相册占用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Album album = new Album();
        BeanUtils.copyProperties(albumUpdateDTO,album);
        album.setId(id);

        log.debug("即将修改数据：{}",album);
        int rows = albumMapper.update(album);
        if (rows != 1){
            String message = "修改相册失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public AlbumStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查询相册详情】的业务，参数：{}",id);
        AlbumStandardVO queryResult = albumMapper.getStandardById(id);
        if (queryResult == null){
            String message = "根据id查询相册详情失败，尝试查询的相册不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        log.debug("即将返回相册详情：{}",queryResult);
        return queryResult;
    }

    @Override
    public List<AlbumListItemVO> list() {
        log.debug("开始处理【查询相册列表】的业务，参数：无");
        return albumMapper.list();
    }


}
